<template>
	<xn-form-container
		:title="formData.id ? '缴纳信息详情' : '互助金缴纳'"
		:width="700"
		v-model:open="open"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-form v-show="formData.id" ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-form-item label="商户订单号：" name="outTradeNo">
				<a-input v-model:value="formData.outTradeNo" placeholder="请输入商户订单号" allow-clear />
			</a-form-item>
			<a-form-item label="缴纳金额：" name="totalFee">
				<a-input v-model:value="formData.totalFee" placeholder="请输入订单金额" allow-clear />
			</a-form-item>
			<a-form-item label="缴纳人员姓名" name="userId">
				<a-input v-model:value="formData.userId" placeholder="请输入订单关联用户的ID" allow-clear />
			</a-form-item>
			<a-form-item label="缴纳人员电话：" name="userrPhone">
				<a-input v-model:value="formData.userrPhone" placeholder="请输入用户电话" allow-clear />
			</a-form-item>
			<a-form-item label="用户关联企业微信ID：" name="userOpenId">
				<a-input v-model:value="formData.userOpenId" placeholder="请输入用户关联企业微信ID" allow-clear />
			</a-form-item>
			<a-form-item label="订单状态：" name="orderStatus">
				<a-input v-model:value="formData.orderStatus" placeholder="请输入订单状态" allow-clear />
			</a-form-item>
			<a-form-item label="订单类型：" name="orderType">
				<a-input v-model:value="formData.orderType" placeholder="请输入订单类型" allow-clear />
			</a-form-item>
			<a-form-item label="付款渠道：" name="bankType">
				<a-input v-model:value="formData.bankType" placeholder="请输入付款渠道" allow-clear />
			</a-form-item>
		</a-form>
		<div v-show="!formData.id">
			<a-flex gap="middle" align="start" vertical>
				<a-flex :style="{ ...boxStyle }" justify="center" align="center">
					<a-card hoverable style="width: 100%; margin: 15px">
						<template #actions>
							<a-radio v-model:checked="wxpayCheck" @change="radioChange('wx')"></a-radio>
						</template>
						<a-card-meta title="微信支付">
							<template #avatar>
								<a-avatar src="/public/img/wx_pay.png" />
							</template>
						</a-card-meta>
					</a-card>
					<a-card hoverable style="width: 100%">
						<template #actions>
							<a-radio v-model:checked="bankPayCheck" @change="radioChange('bank')"></a-radio>
						</template>
						<a-card-meta title="扫码支付">
							<template #avatar>
								<a-avatar src="/public/img/bank_pay.png" />
							</template>
						</a-card-meta>
					</a-card>
				</a-flex>
			</a-flex>
			<a-flex gap="middle" align="start" vertical>
				<a-flex :style="{ ...boxStyle }" justify="center" align="center">
					<a-button type="primary" @click="createOrder" :disabled="payStatus" :loading="payStatus"
						>创建支付订单</a-button
					>
				</a-flex>
			</a-flex>
		</div>

		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" @click="onSubmit" :loading="submitLoading">保存</a-button>
		</template>
	</xn-form-container>
</template>

<script setup name="bizPayOrderForm">
	import { cloneDeep } from 'lodash-es'
	import { required } from '@/utils/formRules'
	import bizPayOrderApi from '@/api/biz/bizPayOrderApi'
	// 抽屉状态
	const open = ref(false)
	const emit = defineEmits({ successful: null })
	const formRef = ref()
	// 表单数据
	const formData = ref({})
	const submitLoading = ref(false)

	// 支付状态选择框
	const wxpayCheck = ref(true)
	const bankPayCheck = ref(false)
	const payStatus = ref(false)

	const boxStyle = {
		width: '100%',
		height: '120px'
	}

	// 打开抽屉
	const onOpen = (record) => {
		open.value = true
		if (record) {
			let recordData = cloneDeep(record)
			formData.value = Object.assign({}, recordData)
		}
	}
	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		formData.value = {}
		open.value = false
	}
	// 默认要校验的
	const formRules = {}
	// 验证并提交数据
	const onSubmit = () => {
		formRef.value
			.validate()
			.then(() => {
				submitLoading.value = true
				const formDataParam = cloneDeep(formData.value)
				bizPayOrderApi
					.bizPayOrderSubmitForm(formDataParam, formDataParam.id)
					.then(() => {
						onClose()
						emit('successful')
					})
					.finally(() => {
						submitLoading.value = false
					})
			})
			.catch(() => {})
	}

	// 选项选择按钮
	const radioChange = (type) => {
		if (type == 'wx') {
			wxpayCheck.value = true
			bankPayCheck.value = false
		} else {
			wxpayCheck.value = false
			bankPayCheck.value = true
		}
	}

	// 订单创建
	const createOrder = async () => {
		payStatus.value = true
		let data = {}

		if (wxpayCheck) {
			data = { payType: 'wx' }
		} else {
			data = { payType: 'bank' }
		}
		console.log(data)
		const res = await bizPayOrderApi.bizPayOrderCreate(data)
		console.log(res)
		payStatus.value = false
	}

	// 抛出函数
	defineExpose({
		onOpen
	})
</script>
